<?php
class Aanchor {
	public static function main($funcode) {
		sview::verify();
		switch ($funcode) {
			case 1:
				self::__doanchor();
				break;
			case 2:
				self::__dostarrank();
				break;
			case 3:
				self::__doviewrank();
				break;
			case 4:
				self::__dosearch();
				break;
			case 5:
				self::__hotsearch();
				break;
			case 6:
				self::__dogetbyids();
				break;
			case 7:
				self::__dorecommond();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//主播搜索2-1-7
	private static function __dorecommond() {
		$acl    = acl::getinst('vedio');
		$dataset= array();
		$fields = 'a.id as studioid,a.userid,a.olstatus,a.iscurated,b.lv as alv,b.xfans,b.starlv';
		$uorm 	= model::load('live/userinfo');
		$myfeeds= empty($acl->user)? array():api_stats::gmyattens($acl->user['id']);
		//每日Top10的主播
		$sorts  = array('a.olstatus'=>orm::DESC, 'a.neartime'=>orm::DESC);
		$where  = array('refid'=>array(0, orm::OP_GT), 'odaycoin'=>array(0, orm::OP_GT));
		$ids    = model::load('live/rank')->get_only_column(0, 10, $where, 'userid', 'odaycoin', orm::DESC);
		if ($ids) {
			$where  = array('a.userid'=>array(implode(',', $ids), orm::OP_IN), 'a.testing'=>array(1, orm::OP_NE));
			$fields = 'a.id as studioid,a.userid,a.olstatus,a.iscurated,b.lv as alv,b.xfans,b.starlv';
			$list   = model::load('live/studio')->index_list(0, 10, $where, $fields, $sorts);
			foreach ($list as &$item) {
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['avtar']    = $user['thumb'];
				$item['coin']     = -1;
				$item['isfollow'] = in_array($item['userid'], $myfeeds)? 1:0;
				$lv               = api_lv::item($item['alv'], 0);
				$item['lvicon']   = $lv['iconsrc'];
				$item['iscurated']= api_studio::grade($item['starlv'], $item['iscurated']);
			}
			$dataset = $list;
		}
		$limit  = 20 - count($dataset);
		//最新开播的主播信息列表
		$where  = array('a.isshow'=>1, 'a.status'=>1, 'a.testing'=>array(1, orm::OP_NE), array('a.neartime', 0, orm::OP_GT));
		if ($ids) {
			$where['a.userid']  = array(implode(',', $ids), orm::OP_NOTIN);
		}
		$list   = model::load('live/studio')->index_list(0, $limit, $where, $fields, $sorts);
		foreach ($list as &$item) {
			$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
			$item['nickname'] = htmlspecialchars_decode($user['nickname']);
			$item['avtar']    = $user['thumb'];
			$item['coin']     = -1;
			$item['isfollow'] = in_array($item['userid'], $myfeeds)? 1:0;
			$lv               = api_lv::item($item['alv'], 0);
			$item['lvicon']   = $lv['iconsrc'];
			$item['iscurated']= api_studio::grade($item['starlv'], $item['iscurated']);
		}
		$dataset = array_merge($dataset, $list);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}
	
	//获取资料信息列表
	private static function __anchorlist($userid, $type, $start, $limit, $sorts, $cond=array()) {
		$where = $cond + array('a.isshow'=>1, 'a.status'=>1, array('a.neartime', 0, orm::OP_GT));
		$fields= 'a.id as studioid,a.userid,a.olstatus,a.iscurated,b.lv,b.xfans,b.starlv';
		$list  = model::load('live/studio')->index_list($start, $limit, $where, $fields, $sorts);
		if ($list) {
			$uorm = model::load('live/userinfo');
			$porm = model::load('live/photo');
			$xfans= array();
			if ($userid > 0) {
				$xfans = api_stats::gmyattens($userid);
			}
			foreach ($list as &$item) {
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$lv    = api_lv::item($user['lv'], 0);
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['avtar']    = $user['thumb'];
				$item['lvicon']   = $lv['iconsrc'];
				$item['type']     = $type;
				$item['studioid'] = intval($item['studioid']);
				$item['lv'] 	  = intval($user['lv']);
				$item['xfans'] 	  = intval($item['xfans']);
				$item['isfollow'] = in_array($item['userid'], $xfans)? 1:0;
				$item['iscurated']= api_studio::grade($item['starlv'], $item['iscurated']);
				//处理相册图片资料信息
				$item['album']    = array();
				$where = array('userid'=>$item['userid']);
				$album = $porm->set_devision($item['userid'])
					->get_only_list(0, 3, $where, 'id,src,note,prise', 'id', orm::DESC);
				if ($album) {
					foreach ($album as &$photo) {
						$photo['smsrc']   = str_replace('.', '_sm.', $photo['src']);
						$photo['isprise'] = 0;
					}
					$item['album'] = $album;
				}
			}
		} else {
			$list = array();
		}
		return $list;
	}
	
	//主播分类查找2-1-1
	private static function __doanchor() {
		$type   = intval(sview::$ctrl['type']);
		$start  = intval(sview::$ctrl['offset']);
		$limit  = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$acl    = acl::getinst('vedio');
		$userid = ($acl->user)? $acl->user['id']:0;
		$list   = array();
		switch ($type) {
			case 1:
				$sorts = array('b.exp'=>orm::DESC, 'a.id'=>orm::DESC);
				$list  = self::__anchorlist($userid, $type, $start, $limit, $sorts);
				break;
			case 2:
				$sorts = array('a.isrecommend'=>orm::DESC, 'a.id'=>orm::DESC);
				$where = array('b.starlv'=>array(4, orm::OP_NE));
				$list  = self::__anchorlist($userid, $type, $start, $limit, $sorts, $where);
				break;
			case 3:
				$sorts = array('a.id'=>orm::DESC);
				$list  = self::__anchorlist($userid, $type, $start, $limit, $sorts);
				break;
			default:
				$sorts = array('b.exp'=>orm::DESC, 'a.id'=>orm::DESC);
				$list1 = self::__anchorlist($userid, 1, $start, $limit, $sorts);
				$sorts = array('a.isrecommend'=>orm::DESC, 'a.id'=>orm::DESC);
				$where = array('b.starlv'=>array(4, orm::OP_NE));
				$list2 = self::__anchorlist($userid, 2, $start, $limit, $sorts, $where);
				$sorts = array('a.id'=>orm::DESC);
				$list3 = self::__anchorlist($userid, 3, $start, $limit, $sorts);
				$list  = array_merge($list1, $list2, $list3);
				break;
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//主播排行版2-1-2
	private static function __dostarrank() {
		$type   = intval(sview::$ctrl['type']);
		$start  = intval(sview::$ctrl['offset']);
		$limit  = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$fields = 'userid,refid as studioid, %s as coin';
		$sorts  = '';
		$where  = array('refid'=>array(0, orm::OP_GT));
		$sorm   = model::load('live/studio');
		$filter = $sorm->get_only_column(0, -1, array('status'=>0), 'userid');
		if ($filter) { //排除官方的主播号
			array_push($filter, 249, 35568);
		} else {
			$filter = array(249, 35568);
		}
		$where['userid']= array($filter, orm::OP_NOTIN);
		switch ($type) {
			case 0:
				$sorts         = 'odaycoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 1:
				$sorts         = 'owekcoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 2:
				$sorts         = 'omthcoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 3:
				$sorts         = 'ocoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			default:
				sview::$view->assign_error(506, lang::text(506));
		}
		$acl    = acl::getinst('vedio');
		$xfans  = array();
		if ($acl->user) {
			$xfans = api_stats::gmyattens($acl->user['id']);
		}
		$fields = sprintf($fields, $sorts);
		$list   = model::load('live/rank')->get_only_list($start, $limit, $where, $fields, $sorts, orm::DESC);
		if ($list) {
			$uorm = model::load('live/userinfo');
			$aorm = model::load('live/anchor');
			$sorm = model::load('live/studio');
			foreach ($list as &$item) {
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$anchor= $aorm->get_one($item['userid']);
				$lv    = api_lv::item($user['lv'], 0);
				$studio= $sorm->get_one($item['studioid']);
				$item['userid']   = intval($item['userid']);
				$item['alv']      = $user['lv'];
				$item['xfans']    = $anchor['xfans'];
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['avtar']    = $user['thumb'];
				$item['lvicon']   = $lv['iconsrc'];
				$item['olstatus'] = $studio['olstatus'];
				$item['userid']   = intval($item['userid']);
				$item['coin']     = -1;//intval($item['coin']);
				$item['studioid'] = intval($item['studioid']);
				$item['isfollow'] = in_array($item['userid'], $xfans)? 1:0;
				$item['iscurated']= api_studio::grade($anchor['starlv'], $studio['iscurated']);
			}
			//usort($list, 'api_studio::lv_sort');
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//观众排行版2-1-3
	private static function __doviewrank() {
		$type   = intval(sview::$ctrl['type']);
		$start  = intval(sview::$ctrl['offset']);
		$limit  = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$fields = 'userid, %s as coin';
		$sorts  = '';
		$where  = array();
		//排除刷榜的用户
		$where[]= array('userid', array(35587), orm::OP_NOTIN);
		switch ($type) {
			case 0:
				$sorts         = 'idaycoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 1:
				$sorts         = 'iwekcoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 2:
				$sorts         = 'imthcoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			case 3:
				$sorts         = 'icoin';
				$where[$sorts] = array(0, orm::OP_GT);
				break;
			default:
				sview::$view->assign_error(506, lang::text(506));
		}
		$fields = sprintf($fields, $sorts);	
		$list   = model::load('live/rank')->get_only_list($start, $limit, $where, $fields, $sorts, orm::DESC);
		if ($list) {
			$uorm = model::load('live/userinfo');
			foreach ($list as &$item) {
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$lv    = api_lv::item($user['lv'], 1);
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['avtar']    = $user['thumb'];
				$item['rlv']      = $user['lv'];
				$item['lvicon']   = $lv['iconsrc'];
				$item['userid']   = intval($item['userid']);
				$item['coin']     = intval($item['coin']);
			}
			//usort($list, 'api_studio::lv_sort');
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//主播搜索2-1-4
	private static function __dosearch() {
		$keywords = trim(sview::$ctrl['keywords']);
		$start    = intval(sview::$ctrl['offset']);
		$limit    = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$nlen     = strlen($keywords);
		if ($nlen < 1 || $nlen > 20) {
			sview::$view->assign_error(506, lang::text(506));
		}
		$acl      = acl::getinst('vedio');
		$myfeeds  = empty($acl->user)? array():api_stats::gmyattens($acl->user['id']);
		$list     = array();
		$user     = api_search::start($keywords, 1, $start, $limit);
		if (isset($user['list']) && !empty($user['list'])) {
			foreach ($user['list'] as $item) {
				$list[] = self::get_anchor($item['userid'], $myfeeds);
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	
	
	//获取直播资料信息
	private static function get_anchor($userid, &$myfeeds) {
		$uorm 	= model::load('live/userinfo');
		$sorm 	= model::load('live/studio');
		$aorm 	= model::load('live/anchor');
		$suser  = $uorm->set_devision($userid)->get_one($userid);
		if (!$suser) {
			return false;
		}
		$anchor = $aorm->get_one($userid);
		$sstud  = $sorm->get_item(array('userid'=>$userid));
		$lv     = api_lv::item($suser['lv'], 0);
		$isfeed = in_array($userid, $myfeeds)? 1:0;
		$iscurated = api_studio::grade($anchor['starlv'], $sstud['iscurated']);
		$data   = array('userid'=>$userid, 'studioid'=>$sstud['id'], 'nickname'=>htmlspecialchars_decode($suser['nickname']),
			'avtar'=>$suser['thumb'], 'xfans'=>$anchor['xfans'], 'olstatus'=>$sstud['olstatus'], 'coin'=>-1, 'isfollow'=>$isfeed,
			'lvicon'=>$lv['iconsrc'], 'alv'=>$suser['lv'], 'iscurated'=>$iscurated
		);
		return $data;
	}
	
	//热门搜索词2-1-5
	private static function __hotsearch() {
		$limit    = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > 30) {
			$limit = 10;
		}
		$where= array();
		$sort = array('rank'=>orm::DESC, 'stime'=>orm::DESC);
		$list = model::load('live/hotsearch')->get_only_column(0, $limit, $where, 'word', $sort);
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//根据id获取主播信息2-1-6
	private static function __dogetbyids() {
		$ids      = trim(sview::$ctrl['ids']);
		$ids      = explode(',', $ids);
		$list     = array();
		$limit    = 30;
		$acl      = acl::getinst('vedio');
		$myfeeds  = empty($acl->user)? array():api_stats::gmyattens($acl->user['id']);
		foreach ($ids as $userid) {
			if (!is_numeric($userid) || $limit < 0) {
				continue;
			}
			$item   = self::get_anchor($userid, $myfeeds);
			if (!$item) {
				continue;
			}
			$limit -= 1;
			$list[] = $item;
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
}